﻿using MediatR;
using THE_WIND_RISES.Common.Dtos;
using Microsoft.AspNetCore.Mvc;
using Production_Get.API.Application.Commands.ResourcteShow;
using Production_Get.API.Application.Commands.WorkCenter_W;
using SlaughterItem.Common.Infrastructures.Interfaces;
using THE_WIND_RISES.Common.Domans.Produce.WorkCenter_;
using NPOI.SS.UserModel;
using NPOI.XSSF.UserModel;
using Microsoft.AspNetCore.StaticFiles;
using Production_Get.API.Infrastructures.Interfaces;

namespace Production_Get.API.Controllers.W
{
    /// <summary>
    /// 资源管理下拉框
    /// </summary>
    [Route("api/[controller]/[action]")]
    [ApiController]
    public class WController : ControllerBase
    {
        private readonly IBaseRepositoryGet<WorkCenter> WorkCenterRepository;
        /// <summary>
        /// 中介者
        /// </summary>
        private readonly IMediator mediator;
        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="mediator"></param>
        /// <param name="logger"></param>
        public WController(IMediator mediator, IBaseRepositoryGet<WorkCenter> workCenterRepository)
        {
            this.mediator = mediator;
            WorkCenterRepository = workCenterRepository;
        }
        /// <summary>
        /// 资源管理下拉框
        /// </summary>
        /// <param name="cmd"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<Result> ResourceShowData(ResourceShowCommand cmd)
        {
            try
            {
                return await mediator.Send(cmd);
            }
            catch (Exception)
            {
                throw;
            }
        }
        /// <summary>
        /// 工作中心显示列表 分页
        /// </summary>
        /// <param name="cmd"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<Result> WorkCenterShowData(WorkCenterShowCommand1 cmd)
        {
            try
            {
                Console.WriteLine("查询工作中心");
                return await mediator.Send(cmd);
            }
            catch (Exception)
            {
                throw;
            }
        }
        /// <summary>
        /// 物料绑定下拉框
        /// </summary>
        /// <param name="cmd"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<Result> ItemInfoShow(ItemInfoShowCommand1 cmd)
        {
            try
            {
                Console.WriteLine("物料绑定下拉框");
                return await mediator.Send(cmd);
            }
            catch (Exception)
            {
                throw;
            }
        }
        /// <summary>
        /// 绑定仓库
        /// </summary>
        /// <param name="cmd"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<Result> WarehouseSelect(WarehouseSelectCommand cmd)
        {
            try
            {
                Console.WriteLine("绑定仓库");
                return await mediator.Send(cmd);
            }
            catch (Exception)
            {
                throw;
            }
        }
        /// <summary>
        /// 库存清单的帆软报表导出
        /// </summary>
        /// <param name="cmd"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<Result> WorMShow(WorMShowCommand cmd)
        {
            try
            {
                Console.WriteLine("库存清单的帆软报表导出");
                return await mediator.Send(cmd);
            }
            catch (Exception)
            {
                throw;
            }
        }
        /// <summary>
        /// 根据条件查询  审批流程表里 对应的编号
        /// </summary>
        /// <param name="cmd"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<Result> WorkflowInstanceShowCommand(WorkflowInstanceShowCommand cmd)
        {
            try
            {
                Console.WriteLine("根据条件查询  审批流程表里 对应的编号");
                return await mediator.Send(cmd);
            }
            catch (Exception)
            {
                throw;
            }
        }
        /// <summary>
        /// 工作中心的导出 xls
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public dynamic WorkCenterExportExcel()
        {
            try
            {
                //查询表信息
                var sql = "select * from workCenters";
                var list = WorkCenterRepository.GetAll(sql, null).Where(x => x.IsDelWorkCenter == false);
                //创建xls文件
                IWorkbook wb = new XSSFWorkbook();
                ISheet sheet = wb.CreateSheet("工作中心信息");
                IRow row = sheet.CreateRow(0);
                //标题
                row.CreateCell(0).SetCellValue("序号");
                row.CreateCell(1).SetCellValue("工作新增编码");
                row.CreateCell(2).SetCellValue("工作新增名称");
                row.CreateCell(3).SetCellValue("工作新增类型");
                row.CreateCell(4).SetCellValue("班次");
                row.CreateCell(5).SetCellValue("创建时间");
                row.CreateCell(6).SetCellValue("创建人");
                row.CreateCell(7).SetCellValue("状态");
                row.CreateCell(8).SetCellValue("审批状态");
                //数据
                var i = 1;
                foreach (var item in list)
                {
                    row = sheet.CreateRow(i);
                    row.CreateCell(0).SetCellValue(item.WorkCenterId);
                    row.CreateCell(1).SetCellValue(item.WorkCenterNum);
                    row.CreateCell(2).SetCellValue(item.WorkCenterName);
                    row.CreateCell(3).SetCellValue(item.WorkCenterType);
                    row.CreateCell(4).SetCellValue(item.ClassesData);
                    row.CreateCell(5).SetCellValue(item.CreateTime.ToString("yyyy-MM-dd-HH-mm-ss"));
                    row.CreateCell(6).SetCellValue(item.CreateName);
                    row.CreateCell(7).SetCellValue(item.WorkCenterState == true ? "有效" : "无效");
                    row.CreateCell(8).SetCellValue(item.ApprovalState == 1 ? "通过" : item.ApprovalState == 2 ? "驳回" : "未审批");
                    i++;
                }
                // 写 WorkBook信息到 内存流中
                byte[] buffer = null;
                using (MemoryStream ms = new MemoryStream())
                {
                    wb.Write(ms);
                    buffer = ms.ToArray();
                }
                // .xlsx文件对应的Mime信息
                var mime = new FileExtensionContentTypeProvider().Mappings[".xlsx"];
                return File(buffer, mime, "工作中心信息.xlsx");
            }
            catch (Exception)
            {
                throw;
            }
        }


    }
}
